/*
 * This file is part of connotea-java.
 *
 * connotea-java is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * connotea-java is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package org.connotea;

import static org.connotea.StringUtils.toBoolean;
import static org.connotea.StringUtils.toInt;

import org.restlet.data.Reference;
import org.restlet.resource.DomRepresentation;
import org.restlet.resource.Representation;
import org.w3c.dom.Node;

/**
 * Parses instances of {@link Message} from XML representations of the same
 * returned as part of calls to the Connotea API.
 * 
 * @author <a href="mailto:christopher.townson@googlemail.com">Christopher
 *         Townson</a>
 */
public class MessageParser {

    /**
     * Parse the provided {@link org.restlet.resource.Representation} as an
     * instance of a {@link Message}.
     * 
     * @param representation the {@link org.restlet.resource.Representation}
     * @return the {@link Message}
     * @see #isValid(Representation)
     */
    public Message parse(Representation representation) {
        if (representation == null) {
            return Message.NULL;
        }
        if (!(isValid(representation))) {
            return Message.INVALID;
        }
        Message message = new Message();
        Node node = new DomRepresentation(representation)
                .getNode("/RDF/Response");
        Node firstChild = node.getFirstChild();
        while (firstChild.getNextSibling() != null) {
            String name = firstChild.getNodeName();
            String text = firstChild.getTextContent();
            if ("code".equals(name)) {
                message.setCode(toInt(text));
            }
            if ("message".equals(name)) {
                message.setMessage(text);
            }
            if ("isSuccess".equals(name)) {
                message.setSuccess(toBoolean(text));
            }
            if ("isFailure".equals(name)) {
                message.setSuccess(!(toBoolean(text)));
            }
            if ("user".equals(name)) {
                message.setUser(text);
            }
            if ("location".equals(name)) {
                message.setLocation(new Reference(text));
            }
            if ("apiVersion".equals(name)) {
                message.setApiVersion(text);
            }
            if ("bibliotechVersion".equals(name)) {
                message.setBibliotechVersion(text);
            }
            firstChild = firstChild.getNextSibling();
        }
        return message;
    }

    /**
     * Determine if the provided {@link org.restlet.resource.Representation} is
     * valid (i.e. can be parsed by this parser).
     * 
     * @param representation the {@link org.restlet.resource.Representation} to
     *            test for parseability
     * @return <code>true</code> if the representation can be parsed as an
     *         instance of a {@link Message}
     */
    public boolean isValid(Representation representation) {
        try {
            DomRepresentation dom = new DomRepresentation(representation);
            return (dom.getDocument() != null)
                    && (dom.getNode("/RDF/Response") != null);
        } catch (Exception e) {
            return false;
        }
    }
}
